using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Webfrom
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            var sql = "select * from BankCard";
            var dt = DbHelper.GetDataTable(sql);

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        //编辑
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            FillData();
        }
        //更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var id = GetValue(e.RowIndex, 0);
            var isOk = int.TryParse(id, out int Id);
            var stuID = GetValue(e.RowIndex, 1);
            var stuName = GetValue(e.RowIndex, 2);
            var stuSex = GetValue(e.RowIndex, 3);
            var stuHobby = GetValue(e.RowIndex, 4);
            var Remarks = GetValue(e.RowIndex, 5);

            if (isOk)
            {
                var sql = string.Format("update BankCard set stuID = '{0}',stuName='{1}',stuSex ='{2}',stuHobby='{3}',Remarks='{4}' where Id = {5}", stuID, stuName, stuSex, stuHobby, Remarks , id);
                DbHelper.AddOrUpdateOrDelete(sql);
            }
            else
            {
                var sql = string.Format("insert into BankCard(stuID,stuName,stuSex,stuHobby,Remarks)values('{0}','{1}','{2}','{3}','{4}')", stuID, stuName, stuSex, stuHobby, Remarks, id);
                DbHelper.AddOrUpdateOrDelete(sql);
            }

            GridView1.EditIndex = -1;
            FillData();
        }

        //删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetValue(e.RowIndex, 0);

            var sql = string.Format("delete from BankCard where Id = {0}", id);
            DbHelper.AddOrUpdateOrDelete(sql);


            GridView1.EditIndex = -1;
            FillData();
        }
        //取消
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            FillData();
        }
        //取值
        private string GetValue(int RowIndex, int ColIndex)
        {
            var cell = GridView1.Rows[RowIndex].Cells[ColIndex];         

            string value = string.Empty;
            if (cell.Controls.Count > 0)
            {
                var t = cell.Controls[0];
                var txtBox = (TextBox)t;
                value = txtBox.Text;
            }
            else
            {
                value = cell.Text;
            }
            return value;
        }
       //添加
        protected void OrAdd_Click(object sender, EventArgs e)
        {
            DataTable data = new DataTable();
            data.Columns.Add(new DataColumn("Id"));
            data.Columns.Add(new DataColumn("stuID"));
            data.Columns.Add(new DataColumn("stuName"));
            data.Columns.Add(new DataColumn("stuSex"));
            data.Columns.Add(new DataColumn("stuHobby"));
            data.Columns.Add(new DataColumn("Remarks"));

            foreach (GridViewRow row in GridView1.Rows)
            {
                var newRow = data.NewRow();
                newRow["Id"] = GetValue(row.RowIndex, 0).Replace("&nbsp;","");
                newRow["stuID"] = GetValue(row.RowIndex, 1).Replace("&nbsp;", "");
                newRow["stuName"] = GetValue(row.RowIndex,2).Replace("&nbsp;", "");  
                newRow["stuSex"] = GetValue(row.RowIndex, 3).Replace("&nbsp;", "");
                newRow["stuHobby"] = GetValue(row.RowIndex, 4).Replace("&nbsp;", "");
                newRow["Remarks"] = GetValue(row.RowIndex, 5).Replace("&nbsp;", "");

                data.Rows.Add(newRow);
            }
            data.Rows.Add(data.NewRow());

            GridView1.EditIndex = data.Rows.Count - 1;

            GridView1.DataSource = data;
            GridView1.DataBind();
        }
    }
}